﻿// ===============================================================================
// XCI.NET 西安交通信息WinForm开发框架 2018 版权所有
// ===============================================================================
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using XCI.Windows.Helper;

namespace XCI.Windows.Controls
{
    /// <summary>
    /// 窗口顶部图片控件
    /// </summary>
    //[System.ComponentModel.DesignerCategory("Code")]
    [DefaultProperty("Text")]
    public class CaptionImage : XtraUserControl
    {
        //private IContainer components;
        private LabelControl labelCaption;
        private PictureBox pictureBoxTop;
        private string text;
        private Font captionFont;
        private Color _captionColor;

        /// <summary>
        /// 
        /// </summary>
        public CaptionImage()
        {
            this.InitializeComponent();
        }

        //protected override void Dispose(bool disposing)
        //{
        //    //if (disposing && (this.components != null))
        //    //{
        //    //    this.components.Dispose();
        //    //}
        //    base.Dispose(disposing);
        //}

        private void CaptionImage_Paint(object sender, PaintEventArgs e)
        {
            var size = WinHelper.GetTextSize(e.Graphics, labelCaption.Text, CaptionFont);
            int top = (pictureBoxTop.Height - Convert.ToInt32(size.Height)) / 2;
            int left = 10;//labelCaption.Left;
            e.Graphics.DrawImage(pictureBoxTop.Image, Width
                - pictureBoxTop.Width, 0, pictureBoxTop.Width, Height);

            if (LogoImage != null)
            {
                int imgTop = (Height - LogoImage.Height) / 2;
                int imgLeft = labelCaption.Left;
                left += LogoImage.Width;
                e.Graphics.DrawImage(LogoImage, imgLeft, imgTop, LogoImage.Width, LogoImage.Height);
            }

            if (!string.IsNullOrEmpty(text))
            {
                e.Graphics.DrawString(text, CaptionFont,new SolidBrush(CaptionColor), left, top);
            }
            
        }

        private void InitializeComponent()
        {
            this.labelCaption = new LabelControl();
            this.pictureBoxTop = new PictureBox();
            ((ISupportInitialize)this.pictureBoxTop).BeginInit();
            this.SuspendLayout();
            this.labelCaption.AutoSize = true;
            this.labelCaption.BackColor = Color.Transparent;
            this.labelCaption.ForeColor = Color.Black;
            this.labelCaption.Location = new Point(20, 10);
            this.labelCaption.Name = "labelCaption";
            //this.labelCaption.Size = new Size(29, 12);
            this.labelCaption.TabIndex = 2;
            this.labelCaption.Text = "标题";
            this.labelCaption.Visible = false;
            this.pictureBoxTop.Dock = DockStyle.Right;
            this.pictureBoxTop.Image = XCI.Windows.Properties.Resources.CaptionImage;
            this.pictureBoxTop.Location = new Point(0, 0);
            this.pictureBoxTop.Name = "pictureBoxTop";
            this.pictureBoxTop.Size = new Size(515, 54);
            this.pictureBoxTop.TabIndex = 1;
            this.pictureBoxTop.TabStop = false;
            this.pictureBoxTop.Visible = false;
            this.pictureBoxTop.LocationChanged +=
                new EventHandler(this.pictureBoxTop_LocationChanged);
            this.AutoScaleMode = AutoScaleMode.None;
            this.BackgroundImage = XCI.Windows.Properties.Resources.CaptionBackground;
            this.Controls.Add(this.labelCaption);
            this.Controls.Add(this.pictureBoxTop);
            this.DoubleBuffered = true;
            this.MaximumSize = new Size(3000, 54);
            this.MinimumSize = new Size(100, 54);
            this.TabStop = false;
            this.Dock = DockStyle.Top;
            this.Name = "CaptionImage";
            this.CaptionText = "标题";
            this.CaptionColor = Color.Black;
            this.Size = new Size(636, 54);
            this.Paint += new PaintEventHandler(this.CaptionImage_Paint);
            ((ISupportInitialize)this.pictureBoxTop).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();
        }

        private void pictureBoxTop_LocationChanged(object sender, EventArgs e)
        {
            this.Invalidate();
        }

        /// <summary>
        /// 指定标题字体
        /// </summary>
        [Description("指定标题字体")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        [Browsable(false)]
        public Font CaptionFont
        {
            get => captionFont ?? (captionFont =
                       new Font(DevExpress.Utils.AppearanceObject.DefaultFont.Name,
                                DevExpress.Utils.AppearanceObject.DefaultFont.Size + 2f,
                                FontStyle.Bold));
            set => captionFont = value;
        }

        /// <summary>
        /// 指定标题颜色
        /// </summary>
        [Description("指定标题颜色")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        [Browsable(true)]
        public Color CaptionColor
        {
            get { return _captionColor; }
            set
            {
                _captionColor = value;
                this.Invalidate();
            }
        }

        /// <summary>
        /// 指定标题文字
        /// </summary>
        [Description("指定标题文字")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        [Browsable(true), DefaultValue("标题文字")]
        public string CaptionText
        {
            get
            {
                return this.text;
            }
            set
            {
                this.text = value;
                this.Invalidate();
            }
        }

        /// <summary>
        /// 标题图片
        /// </summary>
        [Description("指定标题图片")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        [Browsable(true), DefaultValue("标题图片")]
        public Image LogoImage { get; set; }
    }
}